package model.location;

import model.snippet.Snippet;

/**
 * A WorldArea with a depth where can be checked for Snippets.
 */
public class SearchArea extends WorldArea
{	
	private static final float DEPTH_RANGE = 100f;
	
	private int _depth;
	
	public SearchArea(WorldCoordinate upperLeft, Dimension dimension, int depth)
	{
		super((WorldCoordinate) upperLeft, dimension);
		
		_depth = depth;
	}
	
	/**
	 * Checks whether the given snippet is located in the area.
	 * @param snippet The snippet to check.
	 * @return True if the given snippet is located in this SearchArea
	 */
	public boolean contains(Snippet snippet)
	{
		if(isWithinArea(snippet.getWorldArea()))
		{
//			Log.d(TAG, String.format("Snippet: %s - isInRect", snippet.getId()));
			return isWithinDepth((int) ((WorldCoordinate) snippet.getWorldArea().getCenterCoord()).getZ());
		}
		
		return false;
	}
	
	/**
	 * Checks whether given area is within the SearchArea. Not considering the depth.
	 * @param area The area to check.
	 * @return True if the given area fits in the searchArea
	 */
	private boolean isWithinArea(Area area)
	{
		float[] points = area.getPoints();
		
		if(points[0] >= _points[0] && points[2] <= _points[2])
			if(points[1] <= _points[1] && points[7] >= _points[7])
				return true;
		return false; 			
	}
	
	/**
	 * Checks whether given depth is within the depth range.
	 * @param depth The depth to check.
	 * @return True if given depth is within the depth range.
	 */
	private boolean isWithinDepth(int depth)
	{
//		Log.d(TAG, String.format("SearchDepth: %d\nSnippetDepth: %d", _depth, depth));
		return ((depth >= (_depth - DEPTH_RANGE)) && (depth <= (_depth + DEPTH_RANGE)));
	}

	public WorldCoordinate getUpperLeft() 
	{
		return (WorldCoordinate) _coordinate;
	}
	
	public WorldCoordinate getLowerRight()
	{
		return new WorldCoordinate(_coordinate.getX() + _dimension.getWidth(), _coordinate.getY() - _dimension.getHeight(), _depth);
	}
}
